package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Api(tags = "文件上传接口")
@RestController(value = "adminCommonController")
@RequestMapping("/admin/common")
@Slf4j
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        log.info("文件的上传接口被调用了~~");

        int index = file.getOriginalFilename().lastIndexOf(".");
        String endName = file.getOriginalFilename().substring(index);
        String fileName = UUID.randomUUID().toString() + endName;
        log.info("路径：",fileName);
        String path;
        try {
            path = aliOssUtil.upload(file.getBytes(), fileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.success(path);
    }
}
